package neu.dreamera.utils;

/**
 * Created by 10405 on 2016/6/6.
 */

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class HttpConnectionUtil {
    private static final String GET = "GET";
    private static final String POST = "POST";
    private static final String PUT = "PUT";
    private static final String PATCH = "PATCH";
    private static final String DELETE = "DELETE";
    public static  String result = "";
    public static String jsonString = "";

    public static final String SERVERURL = "http://139.129.209.183:5678/cross/";
    public static final String PLACEURL = SERVERURL + "place/";
    public static final String PICTUREURL = SERVERURL + "picture/";

    static String BOUNDARY = java.util.UUID.randomUUID().toString();
//    static String BOUNDARY = "---------------------------7da2137580612";
    static String PREFIX = "--", LINEND = "\r\n";
    static String MULTIPART_FROM_DATA = "multipart/form-data";
    static String CHARSET = "UTF-8";

    private static String prepareParam(Map<String,Object> paramMap){
        StringBuffer sb = new StringBuffer();
        if(paramMap.isEmpty()){
            return "" ;
        }else{
            for(String key: paramMap.keySet()){
                String value = paramMap.get(key).toString();//modified
                if(sb.length()<1){
                    sb.append(key).append("=").append(value);
                }else{
                    sb.append("&").append(key).append("=").append(value);
                }
            }
            return sb.toString();
        }
    }

    public static void doGet(String urlStr, Map<String,Object> paramMap) throws Exception{
        String paramStr = prepareParam(paramMap);
        if(paramStr == null || paramStr.trim().length()<1){

        }else{
            urlStr +="?"+paramStr;
        }
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod(GET);
        conn.setRequestProperty("Content-Type","text/html; charset=UTF-8");
        conn.connect();
        if (conn.getResponseCode() != 200) {
            result = "failed";
            throw new RuntimeException("request failed");
        } else {
            result = "success";
            changeInputToString(conn);
        }
    }

    private static void changeInputToString(HttpURLConnection conn) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = br.readLine()) != null) {
            jsonString += line;
        }
        br.close();
    }

    public static void sendRequest(String urlStr, Map<String,Object> paramMap, String method)
            throws Exception{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setRequestMethod(method);
        String paramStr = prepareParam(paramMap);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        OutputStream os = conn.getOutputStream();
        os.write(paramStr.toString().getBytes("utf-8"));
        os.close();

        changeInputToString(conn);
    }

    public static void doPost(String urlStr,Map<String,Object> paramMap) throws Exception{
        sendRequest(urlStr, paramMap, POST);
    }

    public static void doPut(String urlStr,Map<String,Object> paramMap) throws Exception{
        sendRequest(urlStr, paramMap, PUT);
    }

    public static void doPatch(String urlStr,Map<String,Object> paramMap) throws Exception{
        sendRequest(urlStr, paramMap, PATCH);
    }

    public static void doDelete(String urlStr) throws Exception{
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod(DELETE);

        if(conn.getResponseCode() == 204){ //204 means server return nothing
            result = "success";
        }else{
            System.out.println(conn.getResponseCode());
        }
    }

    public static void doPostPicture(String urlStr, Map<String,Object> paramMap, File pictureFile )
            throws Exception{

        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//        String paramStr = prepareParam(paramMap);

        conn.setDoInput(true);// 允许输入
        conn.setDoOutput(true);// 允许输出
        conn.setUseCaches(false);
        conn.setReadTimeout(10 * 1000); // 缓存的最长时间
        conn.setRequestMethod(POST);

        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);


        DataOutputStream os =  new DataOutputStream(conn.getOutputStream());

        StringBuilder sb1 = new StringBuilder();
        sb1.append(PREFIX);
        sb1.append(BOUNDARY);
        sb1.append(LINEND);
        sb1.append("Content-Disposition: form-data; name=\"picture\"; filename=\"" + pictureFile.getName() + "\"" + LINEND);
        sb1.append("Content-Type: image/jpg; charset=" + CHARSET + LINEND);
        sb1.append(LINEND);
        os.write(sb1.toString().getBytes());
        InputStream is = new FileInputStream(pictureFile);

        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
        }
        is.close();
        os.write(LINEND.getBytes());

        StringBuilder textEntity = new StringBuilder();
        for(Map.Entry<String,Object> entry : paramMap.entrySet()) {
            textEntity.append("--");
            textEntity.append(BOUNDARY);
            textEntity.append("\r\n");
            textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
            textEntity.append(entry.getValue());
            textEntity.append("\r\n");
        }
        os.write(textEntity.toString().getBytes("utf-8"));

//         请求结束标志
        byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
        os.write(end_data);
        os.flush();
        os.close();

        // 得到响应码
        int res = conn.getResponseCode();
        System.out.println("asdf code "+ res);
        System.out.println("asdf " + conn.getResponseMessage());
        conn.disconnect();
    }


    /**
     * 检查当前网络是否可用
     *
     * @param activity
     * @return true or false
     */

    public static boolean isNetworkAvailable(Activity activity) {
        Context context = activity.getApplicationContext();
        // 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null) {
            return false;
        } else {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
            if (networkInfo != null && networkInfo.length > 0) {
                for (int i = 0; i < networkInfo.length; i++) {
//                    System.out.println(i + "===状态===" + networkInfo[i].getState());
//                    System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static ArrayList<HashMap<String, Object>> decodeJsonToPoint(String jsonStr)
            throws JSONException {
        JSONArray jsonArray = null;
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        jsonArray = new JSONArray(jsonStr);
//      System.out.println("asdf json.length = " + jsonArray.length());
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("id", jsonObject.getInt("id"));
            map.put("name", jsonObject.getString("name"));
            map.put("longitude", jsonObject.getString("longitude"));
            map.put("latitude", jsonObject.getString("latitude"));
            map.put("cross_pictures", jsonObject.getString("cross_pictures"));
            list.add(map);
        }
        return list;
    }




}
